// pages/habit_new/habit_new.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    minihabit: {},
    name: "",
    detail: "",
    time_way: "any",
    start_time: "00:00",
    stop_time: "23:59",
    to_show_dates: ["1", "2", "3", "4", "5", "6", "7"],
    select_weekday: [0, 0, 0, 0, 0, 0, 0],
    btn_disabled: false,
    hint: true,
    _id: "",
  },

  delete_habit: function() {
    var that = this

    wx.showModal({
      title: '提示',
      content: '您确定删除这个微习惯么？',
      success: function(res) {
        if (res.confirm) {
          console.log('用户点击确定')
          //删除微习惯
          wx.request({
            url: getApp().globalData.my_server + "/minihabit/delete",
            data: {
              _id: that.data._id
            },
            method: "POST",
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function(res) {
              console.log(res)
              if (res.data.error_no == 2) {
                wx.showModal({
                  title: '哎呀～',
                  content: res.data.error_msg,
                  success: function(res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                    } else if (res.cancel) {
                      console.log('用户点击取消')
                    }
                  }
                })
              } else if (res.data.error_no == 0) {
                wx.navigateBack({
                  delta: 1
                })
              }
            },
            fail: function(res) {
              wx.showModal({
                title: '哎呀～',
                content: '删除微习惯失败',
                success: function(res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
              console.log("fail!", res)
            },
            complete: function(res) {

            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },

  switchChange: function(e) {
    console.log(e.detail.value)
    var that = this
    that.setData({
      hint: e.detail.value
    })

    //设置是否提醒
    wx.request({
      url: getApp().globalData.my_server + "/minihabit/hint",
      data: {
        _id: that.data._id,
        hint: that.data.hint
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)
        if (res.data.error_no == 2) {
          wx.showModal({
            title: '哎呀～',
            content: res.data.error_msg,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {

        }
      },
      fail: function(res) {
        wx.showModal({
          title: '哎呀～',
          content: '设置是否提醒失败',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function(res) {

      }
    })
  },

  submit: function(e) {
    var that = this

    if (that.data.name == "" || that.data.detail == "") {
      wx.showModal({
        title: '哎呀～',
        content: '有的选项没填呢！',
      })
      return
    } else {
      var flag = 0;
      for (var i = 0; i < 7; i++) {
        if (that.data.select_weekday[i]) {
          flag = 1
        }
      }

      if (flag == 0) {
        wx.showModal({
          title: '哎呀～',
          content: '有的选项没填呢！',
        })
        return
      }
    }

    that.setData({
      btn_disabled: true
    })
    wx.showLoading({
      title: '正在提交',
    })

    wx.request({
      url: getApp().globalData.my_server + "/minihabit/update",
      data: {
        name: that.data.name,
        detail: that.data.detail,
        time_way: that.data.time_way,
        start_time: that.data.start_time,
        stop_time: that.data.stop_time,
        select_weekday: that.data.select_weekday,
        _id: that.data.minihabit._id,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)
        if (res.data.error_no == 2) {
          wx.hideLoading()
          that.setData({
            btn_disabled: false
          })
          wx.showModal({
            title: '哎呀～',
            content: res.data.error_msg,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {
          wx.hideLoading()

          wx.navigateBack({
            delta: 1
          })

        }
      },
      fail: function(res) {
        wx.hideLoading()
        that.setData({
          btn_disabled: false
        })
        wx.showModal({
          title: '哎呀～',
          content: '修改微习惯失败，请重新提交',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function(res) {

      }
    })

  },

  select_weekday: function(e) {
    var index = e.currentTarget.dataset.index
    var select_weekday = this.data.select_weekday
    select_weekday[index] = !select_weekday[index]

    this.setData({
      select_weekday: select_weekday
    })
  },

  bindStartTimeChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      start_time: e.detail.value
    })
  },

  bindStopTimeChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      stop_time: e.detail.value
    })
  },

  radioChange: function(e) {
    console.log(e.detail.value)
    this.setData({
      time_way: e.detail.value
    })
  },

  name_input: function(e) {
    this.setData({
      name: e.detail.value
    })
  },

  detail_input: function(e) {
    this.setData({
      detail: e.detail.value
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    that.setData({
      _id: options._id
    })

    //获取详情
    wx.request({
      url: getApp().globalData.my_server + "/minihabit/get_minihabit_details",
      data: {
        _id: that.data._id,
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)
        if (res.data.error_no == 2) {
          wx.showModal({
            title: '哎呀～',
            content: res.data.error_msg,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 0) {
          that.setData({
            minihabit: res.data.minihabit,
            name: res.data.minihabit.name,
            detail: res.data.minihabit.detail,
            time_way: res.data.minihabit.time_way,
            start_time: res.data.minihabit.start_time,
            stop_time: res.data.minihabit.stop_time,
            select_weekday: res.data.minihabit.select_weekday.split(","),
            hint: res.data.minihabit.hint
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '哎呀～',
          content: '获取详情失败',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function(res) {

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})